package com.hd.hdxgddcommon.page;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 自定义分页查询对象适应排序
 * </p>
 *
 * @author Administrator
 * @since 2019-03-18
 */
public class MyPage<T> implements IPage<T> {
    private static final long serialVersionUID = 8545996863226528798L;
    private List<T> records;
    private long total;
    private long size;
    private long current;
    private String[] ascs;
    private String[] descs;
    private boolean optimizeCountSql;
    private boolean isSearchCount;

    public MyPage() {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        this.optimizeCountSql = true;
        this.isSearchCount = true;
    }

    public MyPage(long current, long size) {
        this(current, size, 0L);
    }

    public MyPage(long current, long size, long total) {
        this(current, size, total, true);
    }

    public MyPage(long current, long size, boolean isSearchCount) {
        this(current, size, 0L, isSearchCount);
    }

    public MyPage(long current, long size, long total, boolean isSearchCount) {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        this.optimizeCountSql = true;
        this.isSearchCount = true;
        if (current > 1L) {
            this.current = current;
        }
        this.size = size;
        this.total = total;
        this.isSearchCount = isSearchCount;
    }

    public MyPage(MyPage other, List<T> records) {
        this.records = other.records;
        this.total = other.total;
        this.size = other.size;
        this.current = other.current;
        this.optimizeCountSql = other.optimizeCountSql;
        this.isSearchCount = other.isSearchCount;
        this.size = other.size;
        this.total = other.total;
        this.records = records;
    }

    public boolean hasPrevious() {
        return this.current > 1L;
    }

    public boolean hasNext() {
        return this.current < this.getPages();
    }

    public List<T> getRecords() {
        return this.records;
    }

    public MyPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    public long getTotal() {
        return this.total;
    }

    public MyPage<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    public long getSize() {
        return this.size;
    }

    public MyPage<T> setSize(long size) {
        this.size = size;
        return this;
    }

    public long getCurrent() {
        return this.current;
    }

    public MyPage<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    public String[] ascs() {
        return this.ascs;
    }

    public String[] descs() {
        return this.descs;
    }

    public boolean optimizeCountSql() {
        return this.optimizeCountSql;
    }

    public boolean isSearchCount() {
        return this.total < 0L ? false : this.isSearchCount;
    }

    public MyPage<T> setSearchCount(boolean isSearchCount) {
        this.isSearchCount = isSearchCount;
        return this;
    }

    public MyPage<T> setOptimizeCountSql(boolean optimizeCountSql) {
        this.optimizeCountSql = optimizeCountSql;
        return this;
    }

    public String[] getAscs() {
        return ascs;
    }

    public void setAscs(String[] ascs) {
        this.ascs = ascs;
    }

    public String[] getDescs() {
        return descs;
    }

    public void setDescs(String[] descs) {
        this.descs = descs;
    }

    public Map<String, Object> toPage(IPage<T> page) {
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("itemList", page.getRecords());
        pageMap.put("page", page.getCurrent());
        pageMap.put("page_size", page.getSize());
        pageMap.put("page_total", page.getTotal());
        return pageMap;
    }

    public String toLimitString() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("limit ").append((current - 1) * size).append(", ").append(size);
        return stringBuffer.toString();
    }
}
